package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.statistics;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.CompanyWeightMoneyNumBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.StatisticsBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.DailyStatisticsByCpyExportExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.ProductWeightMoneyNumExportExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CCompanyClassMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.CompanyWeightMoneyNumVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.StatisticsVo;
import com.xiaoxu.intranetweb.util.SzResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@Component
public class StatisticBu {
    @Autowired
    private ICWeightSheetsService icWeightSheetsService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICCompanysService icCompanysService;
    @Autowired
    private ICCompanyClassService icCompanyClassService;
    @Autowired
    private ICOrderDetailService icOrderDetailService;
    @Autowired
    private ICFinaceEnterService icFinaceEnterService;
    @Autowired
    private ICPdtClassService icPdtClassService;
    @Autowired
    private ICProductsInfoService icProductsInfoService;
    @Autowired
    private CCompanyClassMapper cCompanyClassMapper;
//财务管理
    public List<StatisticsVo> financialStatistics(String paymentTimeStart, String paymentTimeEnd, String deliveryTimeStart, String deliveryTimeEnd, Integer cpyClassId, String cpyCo, String cpyChiCo, String cpySupplyCo, String cpySupplyChiCo, Integer pdtClassId, List<String> pdtName, Integer ifDelivery, Integer ifMoney, String token, String plantId) throws Exception {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<StatisticsVo> financialStatisticss = new LinkedList<>();
        if (cpyClassId == null) {
            StatisticsBo statisticsBo = new StatisticsBo();
            LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
            String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            statisticsBo.setLocalDataTime(format);
            if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                statisticsBo.setPaymentTimeStart(paymentTimeStart);
            }
            if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
            }
            if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
            }
            if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
            }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
            if (cpyCo != null && !cpyCo.equals("")) {
                statisticsBo.setCpyCo(cpyCo);
            }
            if (cpyChiCo != null && !cpyChiCo.equals("")) {
                statisticsBo.setCpyChiCo(cpyChiCo);
            }
            if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                statisticsBo.setCpySupplyCo(cpySupplyCo);
            }
            if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
            }
            if (pdtClassId != null) {
                QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
            }
            if (pdtName != null) {
                statisticsBo.setPdtName(pdtName);
            }
            if (ifDelivery != null) {
                statisticsBo.setIfDelivery(ifDelivery);
            }
            if (ifMoney != null) {
                statisticsBo.setIfMoney(ifMoney);
            }
            if (plantId != null && !plantId.equals("")) {
                statisticsBo.setPlantId(plantId);
            }

//        List<StatisticsVo> financialStatisticss = financialStatistics(statisticsBo);
//        financialStatisticss等于financialStatistics(statisticsBo)查询出来的返回值添加进去的
            financialStatisticss = financialStatistics(statisticsBo);
        } else {
//        根据cCompanyClasses里的CPYCLASS_NAME查出所有单位CPY_CO_CLASS
            List<CCompanys> cCompanys = cCompanyClassMapper.getCompanysByClassId(cpyClassId, plantId);
            if (cCompanys.size() == 0) {
                return financialStatisticss;
            }
            for (CCompanys cCompany : cCompanys) {
                StatisticsBo statisticsBo = new StatisticsBo();
                LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
                String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                statisticsBo.setLocalDataTime(format);
                if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                    statisticsBo.setPaymentTimeStart(paymentTimeStart);
                }
                if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                    statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
                }
                if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                    statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
                }
                if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                    statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
                }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
                statisticsBo.setCpyCo(cCompany.getCpyCo());
                statisticsBo.setCpyChiCo(cCompany.getCpyChico());
                if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                    statisticsBo.setCpySupplyCo(cpySupplyCo);
                }
                if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                    statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
                }
                if (pdtClassId != null) {
                    QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                    cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                    cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                    cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                    List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                    List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                    QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                    cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                    List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                    statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                }
                if (pdtName != null) {
                    statisticsBo.setPdtName(pdtName);
                }
                if (ifDelivery != null) {
                    statisticsBo.setIfDelivery(ifDelivery);
                }
                if (ifMoney != null) {
                    statisticsBo.setIfMoney(ifMoney);
                }
                if (plantId != null && !plantId.equals("")) {
                    statisticsBo.setPlantId(plantId);
                }
                financialStatisticss.addAll(financialStatistics(statisticsBo));
            }
        }
        // 首先按照 CpyCo, cpyChiCo, cpySupplyCo, cpySupplyChiCo, pdtName 和 price 排序
        financialStatisticss.sort(Comparator
                .comparing(StatisticsVo::getCpyCo)
                .thenComparing(StatisticsVo::getCpyChiCo)
                .thenComparing(StatisticsVo::getCpySupplyCo)
                .thenComparing(StatisticsVo::getCpySupplyChiCo)
                .thenComparing(StatisticsVo::getPdtName)
                .thenComparing(StatisticsVo::getPrice, Comparator.reverseOrder()));

        // 初始化总计变量
        BigDecimal totalWeight = BigDecimal.ZERO;
        BigDecimal totalMoney = BigDecimal.ZERO;
        Integer totalCarNum = 0;

        // 累加总重量和总金额
        for (StatisticsVo statisticsVo : financialStatisticss) {
            if (statisticsVo.getThisPeriodDelivery() != null) {
                totalWeight = totalWeight.add(statisticsVo.getThisPeriodDelivery());
            }
            if (statisticsVo.getThisPeriodDeliveryMoney() != null) {
                totalMoney = totalMoney.add(statisticsVo.getThisPeriodDeliveryMoney());
            }
            if (statisticsVo.getCarNum() != null) {
                totalCarNum = totalCarNum + statisticsVo.getCarNum();
            }
        }

        // 创建总计行
        StatisticsVo totalRow = createTotalRow(totalCarNum,totalWeight, totalMoney);

        // 将总计行添加到列表末尾
        List<StatisticsVo> result = new ArrayList<>(financialStatisticss);
        result.add(totalRow);

        return result;
    }

    private StatisticsVo createTotalRow(Integer totalCarNum,BigDecimal totalWeight, BigDecimal totalMoney) {
        StatisticsVo totalRow = new StatisticsVo();
        totalRow.setCpyCo("总计"); // 标识为总计
        totalRow.setCarNum(totalCarNum);
        totalRow.setThisPeriodDelivery(totalWeight);
        totalRow.setThisPeriodDeliveryMoney(totalMoney);
        return totalRow;
    }
    //执行的查询
    public List<StatisticsVo> financialStatistics(StatisticsBo statisticsBo) throws Exception {

/**
 *finaceStatistics
 * cpyCo: 单位名称
 * cpyChiCo: 子单位名称
 * thisPeriodRecharge: 本期实交款
 * cpySupplyCo: 供货单位
 * cpySupplyChiCo: 供货子单位
 */
        List<StatisticsVo> thisPeriodRecharges = icFinaceEnterService.finaceStatistics(statisticsBo);
//        System.out.println("thisPeriodRecharges111" + thisPeriodRecharges);
        /**
         * finaceStatisticsEnter
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * thisPeriodAccount: 本期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> thisPeriodAccounts = icFinaceEnterService.finaceStatisticsEnter(statisticsBo);
//        System.out.println("thisPeriodAccounts111" + thisPeriodAccounts);
        /**
         * finaceStatisticsEnterAll
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * allAccount: 累计入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> allAccounts = icFinaceEnterService.finaceStatisticsEnterAll(statisticsBo);
//        System.out.println("allAccounts111" + allAccounts);
        /**
         * finaceStatisticsEnterBefore
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * lastPeriodAccount: 上期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> lastPeriodAccounts = icFinaceEnterService.finaceStatisticsEnterBefore(statisticsBo);
//        System.out.println("lastPeriodAccounts111" + lastPeriodAccounts);
        /**服务于财务统计功能
         * 查询出thisPeriodDeliveryMoney：本期发货金额
         *  thisPeriodDelivery:本期发货量
         *  carNum: 车数
         * price: 单价
         * cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * pdtName: 品种
         **/
        List<StatisticsVo> thisPeriodRechargesBefore = icWeightSheetsService.financialStatistics(statisticsBo);
//        System.out.println("thisPeriodRechargesBefore111" + thisPeriodRechargesBefore);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * lastPeriodConsumption: 截至上期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDeliveryMoney = icWeightSheetsService.financialStatistics2(statisticsBo);
//        System.out.println("thisPeriodDeliveryMoney111" + thisPeriodDeliveryMoney);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * thisPeriodConsumption: 截至本期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDelivery = icWeightSheetsService.financialStatistics3(statisticsBo);
//        System.out.println("thisPeriodDelivery111" + thisPeriodDelivery);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * allConsumption: 不受时间管理的所有消费
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> allConsumption = icWeightSheetsService.financialStatistics4(statisticsBo);
//        System.out.println("allConsumption111" + allConsumption);


//        }
        Map<String, StatisticsVo> totalMap = new HashMap<>();
        for (StatisticsVo vo : thisPeriodRechargesBefore) {
            String key = vo.getCpyCo() + vo.getCpyChiCo() + vo.getCpySupplyCo() + vo.getCpySupplyChiCo();
            StatisticsVo total = totalMap.getOrDefault(key, new StatisticsVo());
            for (StatisticsVo vo1 : allAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllAccount(vo1.getAllAccount());
                }
            }
//            thisPeriodAccounts
            for (StatisticsVo vo1 : thisPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodAccount(vo1.getThisPeriodAccount());
                }
            }
//            thisPeriodRecharges
            for (StatisticsVo vo1 : thisPeriodRecharges) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodRecharge(vo1.getThisPeriodRecharge());
                }
            }
//            lastPeriodAccounts
            for (StatisticsVo vo1 : lastPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodAccount(vo1.getLastPeriodAccount());
                }
            }
//lastPeriodConsumption
            for (StatisticsVo vo1 : thisPeriodDeliveryMoney) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodConsumption(vo1.getLastPeriodConsumption());
                }
            }
//            thisPeriodDeliveryMoney
            for (StatisticsVo vo1 : thisPeriodDelivery) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodConsumption(vo1.getThisPeriodConsumption());
                }
            }
//            allConsumption
            for (StatisticsVo vo1 : allConsumption) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllConsumption(vo1.getAllConsumption());
                }
            }
            //    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//                lastPeriodBalance上期结余额为上期入账-上期消费
//                vo.setLastPeriodBalance(statisticsVo.getLastPeriodAccount().subtract(statisticsVo.getLastPeriodConsumption()));
            if (total.getLastPeriodAccount() != null) {
                if (total.getLastPeriodConsumption() != null) {
                    total.setLastPeriodBalance(total.getLastPeriodAccount().subtract(total.getLastPeriodConsumption()));
                } else {
                    total.setLastPeriodBalance(total.getLastPeriodAccount());
                }
            }
//                thisPeriodBalance本期结余额为本期入账-本期消费
//                vo.setThisPeriodBalance(statisticsVo.getThisPeriodAccount().subtract(statisticsVo.getThisPeriodConsumption()));
            if (total.getThisPeriodAccount() != null) {
                if (total.getThisPeriodConsumption() != null) {
                    total.setThisPeriodBalance(total.getThisPeriodAccount().subtract(total.getThisPeriodConsumption()));
                } else {
                    total.setThisPeriodBalance(total.getThisPeriodAccount());
                }
            }
//balance累计结余为累计入账-累计消费
//                vo.setBalance(statisticsVo.getAllAccount().subtract(statisticsVo.getAllConsumption()));
            //                        有欠款totalDebt
            //                        没有欠款totalDebt
            if (total.getAllAccount() != null) {
                if (total.getAllConsumption() != null) {
                    total.setBalance(total.getAllAccount().subtract(total.getAllConsumption()));
//                        如果为负数，说明有欠款
                    if (total.getBalance().compareTo(new BigDecimal(0)) < 0) {
                        total.setTotalDebt(total.getBalance());
                    } else {
                        total.setTotalDebt(new BigDecimal(0));
                    }
                } else {
                    total.setBalance(vo.getAllAccount());
//                        没有欠款
                    total.setTotalDebt(new BigDecimal(0));
                }
            }
            total.setPdtName("小计");
            total.setCpyCo(vo.getCpyCo());
            total.setCpyChiCo(vo.getCpyChiCo());
            total.setCpySupplyCo(vo.getCpySupplyCo());
            total.setCpySupplyChiCo(vo.getCpySupplyChiCo());
            if (vo.getCarNum() != null) {
                if (total.getCarNum() == null) {
                    total.setCarNum(vo.getCarNum());
                } else {
                    total.setCarNum(total.getCarNum() + vo.getCarNum());
                }
            }

            if (vo.getThisPeriodDeliveryMoney() != null) {
                if (total.getThisPeriodDeliveryMoney() == null) {
                    total.setThisPeriodDeliveryMoney(vo.getThisPeriodDeliveryMoney());
                } else {
                    total.setThisPeriodDeliveryMoney(total.getThisPeriodDeliveryMoney().add(vo.getThisPeriodDeliveryMoney()));
                }
            }

            if (vo.getThisPeriodDelivery() != null) {
                if (total.getThisPeriodDelivery() == null) {
                    total.setThisPeriodDelivery(vo.getThisPeriodDelivery());
                } else {
                    total.setThisPeriodDelivery(total.getThisPeriodDelivery().add(vo.getThisPeriodDelivery()));
                }
            }
//            然后把属于这个小计的值放到 private List<StatisticsVo> statisticsVoList;里面
//            total.getStatisticsVoList().add(vo);
//            初始化 total.getStatisticsVoList()
            if (total.getStatisticsVoList() == null) {
                total.setStatisticsVoList(new ArrayList<>());
            }
            total.getStatisticsVoList().add(vo);
            totalMap.put(key, total);
        }

//   返回totalMap的value
        List<StatisticsVo> totalList = new ArrayList<>();
        for (Map.Entry<String, StatisticsVo> entry : totalMap.entrySet()) {
            totalList.add(entry.getValue());
        }
        return totalList;
    }

    public List<StatisticsVo> exportFinancialStatistics(String paymentTimeStart, String paymentTimeEnd, String deliveryTimeStart, String deliveryTimeEnd, Integer cpyClassId, String cpyCo, String cpyChiCo, String cpySupplyCo, String cpySupplyChiCo, Integer pdtClassId, List<String> pdtName, Integer ifDelivery, Integer ifMoney, String token, String plantId) throws Exception {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<StatisticsVo> financialStatisticss = new LinkedList<>();
        if (cpyClassId == null) {
            StatisticsBo statisticsBo = new StatisticsBo();
            LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
            String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            statisticsBo.setLocalDataTime(format);
            if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                statisticsBo.setPaymentTimeStart(paymentTimeStart);
            }
            if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
            }
            if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
            }
            if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
            }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
            if (cpyCo != null && !cpyCo.equals("")) {
                statisticsBo.setCpyCo(cpyCo);
            }
            if (cpyChiCo != null && !cpyChiCo.equals("")) {
                statisticsBo.setCpyChiCo(cpyChiCo);
            }
            if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                statisticsBo.setCpySupplyCo(cpySupplyCo);
            }
            if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
            }
            if (pdtClassId != null) {
                QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
            }
            if (pdtName != null) {
                statisticsBo.setPdtName(pdtName);
            }
            if (ifDelivery != null) {
                statisticsBo.setIfDelivery(ifDelivery);
            }
            if (ifMoney != null) {
                statisticsBo.setIfMoney(ifMoney);
            }
            if (plantId != null && !plantId.equals("")) {
                statisticsBo.setPlantId(plantId);
            }

//        List<StatisticsVo> financialStatisticss = financialStatistics(statisticsBo);
//        financialStatisticss等于financialStatistics(statisticsBo)查询出来的返回值添加进去的
            financialStatisticss = exportFinancialStatistics(statisticsBo);
        } else {
//        根据cCompanyClasses里的CPYCLASS_NAME查出所有单位CPY_CO_CLASS
            List<CCompanys> cCompanys = cCompanyClassMapper.getCompanysByClassId(cpyClassId, plantId);
            if (cCompanys.size() == 0) {
                return financialStatisticss;
            }
            for (CCompanys cCompany : cCompanys) {
                StatisticsBo statisticsBo = new StatisticsBo();
                LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
                String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                statisticsBo.setLocalDataTime(format);
                if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                    statisticsBo.setPaymentTimeStart(paymentTimeStart);
                }
                if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                    statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
                }
                if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                    statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
                }
                if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                    statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
                }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
                statisticsBo.setCpyCo(cCompany.getCpyCo());
                statisticsBo.setCpyChiCo(cCompany.getCpyChico());
                if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                    statisticsBo.setCpySupplyCo(cpySupplyCo);
                }
                if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                    statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
                }
                if (pdtClassId != null) {
                    QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                    cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                    cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                    List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                    List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                    QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                    cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                    List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                    statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                }
                if (pdtName != null) {
                    statisticsBo.setPdtName(pdtName);
                }
                if (ifDelivery != null) {
                    statisticsBo.setIfDelivery(ifDelivery);
                }
                if (ifMoney != null) {
                    statisticsBo.setIfMoney(ifMoney);
                }
                if (plantId != null && !plantId.equals("")) {
                    statisticsBo.setPlantId(plantId);
                }
                financialStatisticss.addAll(exportFinancialStatistics(statisticsBo));
            }
        }
        //        最后thisPeriodRechargesBefore按照cpyCo, cpyChiCo, cpySupplyCo, cpySupplyChiCo,pdtName,最后price从大到小排序，最重要的是将小计放到最前面

// 使用原始比较器进行初步排序
        Comparator<StatisticsVo> customComparator = Comparator
                .comparing(StatisticsVo::getCpyCo)
                .thenComparing(StatisticsVo::getCpyChiCo)
                .thenComparing(StatisticsVo::getCpySupplyCo)
                .thenComparing(StatisticsVo::getCpySupplyChiCo);

// 对列表进行初步排序，仅根据分组字段
        financialStatisticss.sort(customComparator);

// 再次分组并调整“小计”位置，同时保证PdtName的排序
        List<StatisticsVo> finalSorted = financialStatisticss.stream()
                .collect(Collectors.groupingBy(statisticsVo -> Arrays.asList(
                        statisticsVo.getCpyCo(),
                        statisticsVo.getCpyChiCo(),
                        statisticsVo.getCpySupplyCo(),
                        statisticsVo.getCpySupplyChiCo()
                ), LinkedHashMap::new, Collectors.toList())) // 保持插入顺序
                .values().stream()
                .flatMap(group -> {
                    // 先根据 PdtName 和 getThisPeriodDelivery 对分组内的条目排序
                    group.sort(Comparator.comparing(StatisticsVo::getPdtName)
                            .thenComparing(StatisticsVo::getThisPeriodDelivery));

                    // 分离出“小计”项和其他项
                    List<StatisticsVo> subtotalItems = group.stream()
                            .filter(statisticsVo -> statisticsVo.getPdtName() != null && statisticsVo.getPdtName().trim().toLowerCase().contains("小计"))
                            .collect(Collectors.toList());
                    List<StatisticsVo> otherItems = group.stream()
                            .filter(statisticsVo -> statisticsVo.getPdtName() == null || !statisticsVo.getPdtName().trim().toLowerCase().contains("小计"))
                            .collect(Collectors.toList());

                    // 合并其他项和“小计”项
                    List<StatisticsVo> combined = new ArrayList<>(otherItems);
                    combined.addAll(subtotalItems);
                    return combined.stream();
                })
                .collect(Collectors.toList());

// 更新原始列表
        financialStatisticss.clear();
        financialStatisticss.addAll(finalSorted);

        return financialStatisticss;
    }

    //执行的查询
    public List<StatisticsVo> exportFinancialStatistics(StatisticsBo statisticsBo) throws Exception {

/**
 *finaceStatistics
 * cpyCo: 单位名称
 * cpyChiCo: 子单位名称
 * thisPeriodRecharge: 本期实交款
 * cpySupplyCo: 供货单位
 * cpySupplyChiCo: 供货子单位
 */
        List<StatisticsVo> thisPeriodRecharges = icFinaceEnterService.finaceStatistics(statisticsBo);
//        System.out.println("thisPeriodRecharges111" + thisPeriodRecharges);
        /**
         * finaceStatisticsEnter
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * thisPeriodAccount: 本期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> thisPeriodAccounts = icFinaceEnterService.finaceStatisticsEnter(statisticsBo);
//        System.out.println("thisPeriodAccounts111" + thisPeriodAccounts);
        /**
         * finaceStatisticsEnterAll
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * allAccount: 累计入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> allAccounts = icFinaceEnterService.finaceStatisticsEnterAll(statisticsBo);
//        System.out.println("allAccounts111" + allAccounts);
        /**
         * finaceStatisticsEnterBefore
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * lastPeriodAccount: 上期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> lastPeriodAccounts = icFinaceEnterService.finaceStatisticsEnterBefore(statisticsBo);
//        System.out.println("lastPeriodAccounts111" + lastPeriodAccounts);
        /**服务于财务统计功能
         * 查询出thisPeriodDeliveryMoney：本期发货金额
         *  thisPeriodDelivery:本期发货量
         *  carNum: 车数
         * price: 单价
         * cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * pdtName: 品种
         **/
        List<StatisticsVo> thisPeriodRechargesBefore = icWeightSheetsService.financialStatistics(statisticsBo);
//        System.out.println("thisPeriodRechargesBefore111" + thisPeriodRechargesBefore);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * lastPeriodConsumption: 截至上期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDeliveryMoney = icWeightSheetsService.financialStatistics2(statisticsBo);
//        System.out.println("thisPeriodDeliveryMoney111" + thisPeriodDeliveryMoney);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * thisPeriodConsumption: 截至本期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDelivery = icWeightSheetsService.financialStatistics3(statisticsBo);
//        System.out.println("thisPeriodDelivery111" + thisPeriodDelivery);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * allConsumption: 不受时间管理的所有消费
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> allConsumption = icWeightSheetsService.financialStatistics4(statisticsBo);
//        System.out.println("allConsumption111" + allConsumption);


//        }
        Map<String, StatisticsVo> totalMap = new HashMap<>();
        for (StatisticsVo vo : thisPeriodRechargesBefore) {
            String key = vo.getCpyCo() + vo.getCpyChiCo() + vo.getCpySupplyCo() + vo.getCpySupplyChiCo();
            StatisticsVo total = totalMap.getOrDefault(key, new StatisticsVo());
            vo.setCpyName(vo.getCpyCo() + "." + vo.getCpyChiCo());
            vo.setCpySupplyCoName(vo.getCpySupplyCo() + "." + vo.getCpySupplyChiCo());
            for (StatisticsVo vo1 : allAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllAccount(vo1.getAllAccount());
                }
            }
//            thisPeriodAccounts
            for (StatisticsVo vo1 : thisPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodAccount(vo1.getThisPeriodAccount());
                }
            }
//            thisPeriodRecharges
            for (StatisticsVo vo1 : thisPeriodRecharges) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodRecharge(vo1.getThisPeriodRecharge());
                }
            }
//            lastPeriodAccounts
            for (StatisticsVo vo1 : lastPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodAccount(vo1.getLastPeriodAccount());
                }
            }
//lastPeriodConsumption
            for (StatisticsVo vo1 : thisPeriodDeliveryMoney) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodConsumption(vo1.getLastPeriodConsumption());
                }
            }
//            thisPeriodDeliveryMoney
            for (StatisticsVo vo1 : thisPeriodDelivery) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodConsumption(vo1.getThisPeriodConsumption());
                }
            }
//            allConsumption
            for (StatisticsVo vo1 : allConsumption) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllConsumption(vo1.getAllConsumption());
                }
            }
            //    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//                lastPeriodBalance上期结余额为上期入账-上期消费
//                vo.setLastPeriodBalance(statisticsVo.getLastPeriodAccount().subtract(statisticsVo.getLastPeriodConsumption()));
            if (total.getLastPeriodAccount() != null) {
                if (total.getLastPeriodConsumption() != null) {
                    total.setLastPeriodBalance(total.getLastPeriodAccount().subtract(total.getLastPeriodConsumption()));
                } else {
                    total.setLastPeriodBalance(total.getLastPeriodAccount());
                }
            }
//                thisPeriodBalance本期结余额为本期入账-本期消费
//                vo.setThisPeriodBalance(statisticsVo.getThisPeriodAccount().subtract(statisticsVo.getThisPeriodConsumption()));
            if (total.getThisPeriodAccount() != null) {
                if (total.getThisPeriodConsumption() != null) {
                    total.setThisPeriodBalance(total.getThisPeriodAccount().subtract(total.getThisPeriodConsumption()));
                } else {
                    total.setThisPeriodBalance(total.getThisPeriodAccount());
                }
            }
//balance累计结余为累计入账-累计消费
//                vo.setBalance(statisticsVo.getAllAccount().subtract(statisticsVo.getAllConsumption()));
            //                        有欠款totalDebt
            //                        没有欠款totalDebt
            if (total.getAllAccount() != null) {
                if (total.getAllConsumption() != null) {
                    total.setBalance(total.getAllAccount().subtract(total.getAllConsumption()));
//                        如果为负数，说明有欠款
                    if (total.getBalance().compareTo(new BigDecimal(0)) < 0) {
                        total.setTotalDebt(total.getBalance());
                    } else {
                        total.setTotalDebt(new BigDecimal(0));
                    }
                } else {
                    total.setBalance(vo.getAllAccount());
//                        没有欠款
                    total.setTotalDebt(new BigDecimal(0));
                }
            }
            total.setPdtName("小计");
            total.setCpyName(vo.getCpyCo() + "." + vo.getCpyChiCo());
            total.setCpySupplyCoName(vo.getCpySupplyCo() + "." + vo.getCpySupplyChiCo());
            total.setCpyCo(vo.getCpyCo());
            total.setCpyChiCo(vo.getCpyChiCo());
            total.setCpySupplyCo(vo.getCpySupplyCo());
            total.setCpySupplyChiCo(vo.getCpySupplyChiCo());
            if (vo.getCarNum() != null) {
                if (total.getCarNum() == null) {
                    total.setCarNum(vo.getCarNum());
                } else {
                    total.setCarNum(total.getCarNum() + vo.getCarNum());
                }
            }

            if (vo.getThisPeriodDeliveryMoney() != null) {
                if (total.getThisPeriodDeliveryMoney() == null) {
                    total.setThisPeriodDeliveryMoney(vo.getThisPeriodDeliveryMoney());
                } else {
                    total.setThisPeriodDeliveryMoney(total.getThisPeriodDeliveryMoney().add(vo.getThisPeriodDeliveryMoney()));
                }
            }

            if (vo.getThisPeriodDelivery() != null) {
                if (total.getThisPeriodDelivery() == null) {
                    total.setThisPeriodDelivery(vo.getThisPeriodDelivery());
                } else {
                    total.setThisPeriodDelivery(total.getThisPeriodDelivery().add(vo.getThisPeriodDelivery()));
                }
            }
            totalMap.put(key, total);
        }
//     把totalMap放到thisPeriodRechargesBefore中
        List<StatisticsVo> totalList = new ArrayList<>();
        for (Map.Entry<String, StatisticsVo> entry : totalMap.entrySet()) {
            totalList.add(entry.getValue());
        }

        thisPeriodRechargesBefore.addAll(totalList);
        return thisPeriodRechargesBefore;
    }

    //   exportFinancialStatisticsSettlement
    public List<StatisticsVo> exportFinancialStatisticsSettlement(String paymentTimeStart, String paymentTimeEnd, String deliveryTimeStart, String deliveryTimeEnd, Integer cpyClassId, String cpyCo, String cpyChiCo, String cpySupplyCo, String cpySupplyChiCo, Integer pdtClassId, List<String> pdtName, Integer ifDelivery, Integer ifMoney, String token, String plantId) throws Exception {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<StatisticsVo> financialStatisticss = new LinkedList<>();
        if (cpyClassId == null) {
            StatisticsBo statisticsBo = new StatisticsBo();
            LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
            String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            statisticsBo.setLocalDataTime(format);
            if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                statisticsBo.setPaymentTimeStart(paymentTimeStart);
            }
            if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
            }
            if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
            }
            if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
            }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
            if (cpyCo != null && !cpyCo.equals("")) {
                statisticsBo.setCpyCo(cpyCo);
            }
            if (cpyChiCo != null && !cpyChiCo.equals("")) {
                statisticsBo.setCpyChiCo(cpyChiCo);
            }
            if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                statisticsBo.setCpySupplyCo(cpySupplyCo);
            }
            if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
            }
            if (pdtClassId != null) {
                QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
            }
            if (pdtName != null) {
                statisticsBo.setPdtName(pdtName);
            }
            if (ifDelivery != null) {
                statisticsBo.setIfDelivery(ifDelivery);
            }
            if (ifMoney != null) {
                statisticsBo.setIfMoney(ifMoney);
            }
            if (plantId != null && !plantId.equals("")) {
                statisticsBo.setPlantId(plantId);
            }

//        List<StatisticsVo> financialStatisticss = financialStatistics(statisticsBo);
//        financialStatisticss等于financialStatistics(statisticsBo)查询出来的返回值添加进去的
            financialStatisticss = financialStatisticsSettlement(statisticsBo);
        } else {
//        根据cCompanyClasses里的CPYCLASS_NAME查出所有单位CPY_CO_CLASS
            List<CCompanys> cCompanys = cCompanyClassMapper.getCompanysByClassId(cpyClassId, plantId);
            if (cCompanys.size() == 0) {
                return financialStatisticss;
            }
            for (CCompanys cCompany : cCompanys) {
                StatisticsBo statisticsBo = new StatisticsBo();
                LocalDateTime LocalDateTime = java.time.LocalDateTime.now();
                String format = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                statisticsBo.setLocalDataTime(format);
                if (paymentTimeStart != null && !paymentTimeStart.equals("")) {
                    statisticsBo.setPaymentTimeStart(paymentTimeStart);
                }
                if (paymentTimeEnd != null && !paymentTimeEnd.equals("")) {
                    statisticsBo.setPaymentTimeEnd(paymentTimeEnd);
                }
                if (deliveryTimeStart != null && !deliveryTimeStart.equals("")) {
                    statisticsBo.setDeliveryTimeStart(deliveryTimeStart);
                }
                if (deliveryTimeEnd != null && !deliveryTimeEnd.equals("")) {
                    statisticsBo.setDeliveryTimeEnd(deliveryTimeEnd);
                }
//        if (cpyType != null && !cpyType.equals("")) {
//            statisticsBo.setCpyType(cpyType);
//        }
                statisticsBo.setCpyCo(cCompany.getCpyCo());
                statisticsBo.setCpyChiCo(cCompany.getCpyChico());
                if (cpySupplyCo != null && !cpySupplyCo.equals("")) {
                    statisticsBo.setCpySupplyCo(cpySupplyCo);
                }
                if (cpySupplyChiCo != null && !cpySupplyChiCo.equals("")) {
                    statisticsBo.setCpySupplyChiCo(cpySupplyChiCo);
                }
                if (pdtClassId != null) {
                    QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                    cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                    cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                    cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                    List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                    List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                    QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                    cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                    List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                    statisticsBo.setPdtName(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                }
                if (pdtName != null) {
                    statisticsBo.setPdtName(pdtName);
                }
                if (ifDelivery != null) {
                    statisticsBo.setIfDelivery(ifDelivery);
                }
                if (ifMoney != null) {
                    statisticsBo.setIfMoney(ifMoney);
                }
                if (plantId != null && !plantId.equals("")) {
                    statisticsBo.setPlantId(plantId);
                }
                financialStatisticss.addAll(financialStatisticsSettlement(statisticsBo));
            }
        }
        //        最后thisPeriodRechargesBefore按照cpyCo, cpyChiCo, cpySupplyCo, cpySupplyChiCo,pdtName,最后price从大到小排序，最重要的是将小计放到最前面
        financialStatisticss.sort(Comparator
                .comparing(StatisticsVo::getCpyCo)
                .thenComparing(StatisticsVo::getCpyChiCo)
                .thenComparing(StatisticsVo::getCpySupplyCo)
                .thenComparing(StatisticsVo::getCpySupplyChiCo)
                .thenComparing(StatisticsVo::getPdtName)
                .thenComparing(StatisticsVo::getPrice, Comparator.reverseOrder()));
        for (StatisticsVo financialStatistics : financialStatisticss) {
            financialStatistics.getStatisticsVoList().sort(Comparator
                    .comparing(StatisticsVo::getCpyCo)
                    .thenComparing(StatisticsVo::getCpyChiCo)
                    .thenComparing(StatisticsVo::getCpySupplyCo)
                    .thenComparing(StatisticsVo::getCpySupplyChiCo)
                    .thenComparing(StatisticsVo::getPdtName)
                    .thenComparing(StatisticsVo::getPrice, Comparator.reverseOrder()));
        }
//         financialStatisticss.statisticsVoList也要按照cpyCo, cpyChiCo, cpySupplyCo, cpySupplyChiCo,pdtName,最后price从大到小排序
        return financialStatisticss;
    }

    //执行的查询
    public List<StatisticsVo> financialStatisticsSettlement(StatisticsBo statisticsBo) throws Exception {

/**
 *finaceStatistics
 * cpyCo: 单位名称
 * cpyChiCo: 子单位名称
 * thisPeriodRecharge: 本期实交款
 * cpySupplyCo: 供货单位
 * cpySupplyChiCo: 供货子单位
 */
        List<StatisticsVo> thisPeriodRecharges = icFinaceEnterService.finaceStatistics(statisticsBo);
//        System.out.println("thisPeriodRecharges111" + thisPeriodRecharges);
        /**
         * finaceStatisticsEnter
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * thisPeriodAccount: 本期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> thisPeriodAccounts = icFinaceEnterService.finaceStatisticsEnter(statisticsBo);
//        System.out.println("thisPeriodAccounts111" + thisPeriodAccounts);
        /**
         * finaceStatisticsEnterAll
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * allAccount: 累计入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> allAccounts = icFinaceEnterService.finaceStatisticsEnterAll(statisticsBo);
//        System.out.println("allAccounts111" + allAccounts);
        /**
         * finaceStatisticsEnterBefore
         * cpyCo: 单位名称
         * cpyChiCo: 子单位名称
         * lastPeriodAccount: 上期入账
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         */
        List<StatisticsVo> lastPeriodAccounts = icFinaceEnterService.finaceStatisticsEnterBefore(statisticsBo);
//        System.out.println("lastPeriodAccounts111" + lastPeriodAccounts);
        /**服务于财务统计功能
         * 查询出thisPeriodDeliveryMoney：本期发货金额
         *  thisPeriodDelivery:本期发货量
         *  carNum: 车数
         * price: 单价
         * cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * pdtName: 品种
         **/
        List<StatisticsVo> thisPeriodRechargesBefore = icWeightSheetsService.financialStatistics(statisticsBo);
//        System.out.println("thisPeriodRechargesBefore111" + thisPeriodRechargesBefore);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * lastPeriodConsumption: 截至上期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDeliveryMoney = icWeightSheetsService.financialStatistics2(statisticsBo);
//        System.out.println("thisPeriodDeliveryMoney111" + thisPeriodDeliveryMoney);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * thisPeriodConsumption: 截至本期累计消费金额
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> thisPeriodDelivery = icWeightSheetsService.financialStatistics3(statisticsBo);
//        System.out.println("thisPeriodDelivery111" + thisPeriodDelivery);
        /**服务于财务统计功能
         * 查询出cpyCo: 收货单位
         * cpyChiCo: 收货子单位
         * cpySupplyCo: 供货单位
         * cpySupplyChiCo: 供货子单位
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * cpyCo=测试单位, cpyChiCo=，, cpySupplyCo=华能济宁运河发电有限公司, cpySupplyChiCo=，,
         * allConsumption: 不受时间管理的所有消费
         * * pdtName: 品种
         * @param params
         * @return
         */
        List<StatisticsVo> allConsumption = icWeightSheetsService.financialStatistics4(statisticsBo);
//        System.out.println("allConsumption111" + allConsumption);


//        }
        Map<String, StatisticsVo> totalMap = new HashMap<>();
        for (StatisticsVo vo : thisPeriodRechargesBefore) {
            String key = vo.getCpyCo() + vo.getCpyChiCo() + vo.getCpySupplyCo() + vo.getCpySupplyChiCo();
            StatisticsVo total = totalMap.getOrDefault(key, new StatisticsVo());
            vo.setCpyName(vo.getCpyCo() + "." + vo.getCpyChiCo());
            vo.setCpySupplyCoName(vo.getCpySupplyCo() + "." + vo.getCpySupplyChiCo());
            for (StatisticsVo vo1 : allAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllAccount(vo1.getAllAccount());
                }
            }
//            thisPeriodAccounts
            for (StatisticsVo vo1 : thisPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodAccount(vo1.getThisPeriodAccount());
                }
            }
//            thisPeriodRecharges
            for (StatisticsVo vo1 : thisPeriodRecharges) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodRecharge(vo1.getThisPeriodRecharge());
                }
            }
//            lastPeriodAccounts
            for (StatisticsVo vo1 : lastPeriodAccounts) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodAccount(vo1.getLastPeriodAccount());
                }
            }
//lastPeriodConsumption
            for (StatisticsVo vo1 : thisPeriodDeliveryMoney) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setLastPeriodConsumption(vo1.getLastPeriodConsumption());
                }
            }
//            thisPeriodDeliveryMoney
            for (StatisticsVo vo1 : thisPeriodDelivery) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setThisPeriodConsumption(vo1.getThisPeriodConsumption());
                }
            }
//            allConsumption
            for (StatisticsVo vo1 : allConsumption) {
                String key1 = vo1.getCpyCo() + vo1.getCpyChiCo() + vo1.getCpySupplyCo() + vo1.getCpySupplyChiCo();
                if (key.equals(key1)) {
                    total.setAllConsumption(vo1.getAllConsumption());
                }
            }
            //    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//                lastPeriodBalance上期结余额为上期入账-上期消费
//                vo.setLastPeriodBalance(statisticsVo.getLastPeriodAccount().subtract(statisticsVo.getLastPeriodConsumption()));
            if (total.getLastPeriodAccount() != null) {
                if (total.getLastPeriodConsumption() != null) {
                    total.setLastPeriodBalance(total.getLastPeriodAccount().subtract(total.getLastPeriodConsumption()));
                } else {
                    total.setLastPeriodBalance(total.getLastPeriodAccount());
                }
            }
//                thisPeriodBalance本期结余额为本期入账-本期消费
//                vo.setThisPeriodBalance(statisticsVo.getThisPeriodAccount().subtract(statisticsVo.getThisPeriodConsumption()));
            if (total.getThisPeriodAccount() != null) {
                if (total.getThisPeriodConsumption() != null) {
                    total.setThisPeriodBalance(total.getThisPeriodAccount().subtract(total.getThisPeriodConsumption()));
                } else {
                    total.setThisPeriodBalance(total.getThisPeriodAccount());
                }
            }
//balance累计结余为累计入账-累计消费
//                vo.setBalance(statisticsVo.getAllAccount().subtract(statisticsVo.getAllConsumption()));
            //                        有欠款totalDebt
            //                        没有欠款totalDebt
            if (total.getAllAccount() != null) {
                if (total.getAllConsumption() != null) {
                    total.setBalance(total.getAllAccount().subtract(total.getAllConsumption()));
//                        如果为负数，说明有欠款
                    if (total.getBalance().compareTo(new BigDecimal(0)) < 0) {
                        total.setTotalDebt(total.getBalance());
                    } else {
                        total.setTotalDebt(new BigDecimal(0));
                    }
                } else {
                    total.setBalance(vo.getAllAccount());
//                        没有欠款
                    total.setTotalDebt(new BigDecimal(0));
                }
            }
            total.setPdtName("小计");
            total.setCpyName(vo.getCpyCo() + "." + vo.getCpyChiCo());
            total.setCpySupplyCoName(vo.getCpySupplyCo() + "." + vo.getCpySupplyChiCo());
            total.setCpyCo(vo.getCpyCo());
            total.setCpyChiCo(vo.getCpyChiCo());
            total.setCpySupplyCo(vo.getCpySupplyCo());
            total.setCpySupplyChiCo(vo.getCpySupplyChiCo());
            if (vo.getCarNum() != null) {
                if (total.getCarNum() == null) {
                    total.setCarNum(vo.getCarNum());
                } else {
                    total.setCarNum(total.getCarNum() + vo.getCarNum());
                }
            }

            if (vo.getThisPeriodDeliveryMoney() != null) {
                if (total.getThisPeriodDeliveryMoney() == null) {
                    total.setThisPeriodDeliveryMoney(vo.getThisPeriodDeliveryMoney());
                } else {
                    total.setThisPeriodDeliveryMoney(total.getThisPeriodDeliveryMoney().add(vo.getThisPeriodDeliveryMoney()));
                }
            }

            if (vo.getThisPeriodDelivery() != null) {
                if (total.getThisPeriodDelivery() == null) {
                    total.setThisPeriodDelivery(vo.getThisPeriodDelivery());
                } else {
                    total.setThisPeriodDelivery(total.getThisPeriodDelivery().add(vo.getThisPeriodDelivery()));
                }
            }
//            然后把属于这个小计的值放到 private List<StatisticsVo> statisticsVoList;里面
//            total.getStatisticsVoList().add(vo);
//            初始化 total.getStatisticsVoList()
            if (total.getStatisticsVoList() == null) {
                total.setStatisticsVoList(new ArrayList<>());
            }
            total.getStatisticsVoList().add(vo);
            totalMap.put(key, total);
        }

//   返回totalMap的value
        List<StatisticsVo> totalList = new ArrayList<>();
        for (Map.Entry<String, StatisticsVo> entry : totalMap.entrySet()) {
            totalList.add(entry.getValue());
        }
        return totalList;
    }


    /**
     * 每日统计
     * 每日统计
     * dailyStatistics
     */
    public SzResult dailyStatistics(String dateTime, String coSupply, String coSupplyChi, String co, String chiCo, Integer pdtClassId, List<String> product, String truckNo, String iccardno, Integer isEmpty, String cpyClass, String pdtClass, String token, String plantId) throws Exception {
        // String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
        if (coSupply != null && !coSupply.equals("")) {
            companyWeightMoneyNumBO.setCoSupply(coSupply);
        }
        if (coSupplyChi != null && !coSupplyChi.equals("")) {
            companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
        }
        if (co != null && !co.equals("")) {
            companyWeightMoneyNumBO.setCo(co);
        }
        if (chiCo != null && !chiCo.equals("")) {
            companyWeightMoneyNumBO.setChiCo(chiCo);
        }
        if (pdtClassId != null) {
            QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
            cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
            cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
            cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
            List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
            List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
            companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
        }
        if (product != null && product.size() > 0) {
            companyWeightMoneyNumBO.setProduct(product);
        }
        if (dateTime != null && !dateTime.equals("")) {
            String startTime = dateTime + " 00:00:00";
            String endTime = dateTime + " 23:59:59";
            companyWeightMoneyNumBO.setStartTime(startTime);
            companyWeightMoneyNumBO.setEndTime(endTime);
        }
        if (truckNo != null && !truckNo.equals("")) {
            companyWeightMoneyNumBO.setTruckNo(truckNo);
        }
        if (iccardno != null && !iccardno.equals("")) {
            companyWeightMoneyNumBO.setIccardno(iccardno);
        }

        if (isEmpty != null) {
            companyWeightMoneyNumBO.setIsEmpty(isEmpty);
        }
        if (plantId != null && !plantId.equals("")) {
            companyWeightMoneyNumBO.setPlantId(plantId);
        }
//        默认为不查询空车
        else {
            companyWeightMoneyNumBO.setIsEmpty(0);
        }
        List<CompanyWeightMoneyNumVo> companyWeightMoneyNum = icWeightSheetsService.dailyStatistics(companyWeightMoneyNumBO);
        return SzResult.success(companyWeightMoneyNum);
    }


    /**
     * 日报统计导表
     */
    @Async
    public Future<List<ProductWeightMoneyNumExportExcelEn>> dailyStatisticsExportExcel(List<Map<String, Object>> fields, List<Map<String, Object>> ids, String filename, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, Integer pdtClassId, List<String> product, String startTime, String endTime, String truckNo, String icCardNo, String sheetName, HttpServletResponse response, String plantId, String coSupply, String coSupplyChi, String co, String coChi) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<ProductWeightMoneyNumExportExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        ProductWeightMoneyNumExportExcelEn productWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }
                    break;
                case "all":
                    CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
                    if (coSupply != null && !coSupply.equals("")) {
                        companyWeightMoneyNumBO.setCoSupply(coSupply);
                    }
                    if (coSupplyChi != null && !coSupplyChi.equals("")) {
                        companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
                    }
                    if (co != null && !co.equals("")) {
                        companyWeightMoneyNumBO.setCo(co);
                    }
                    if (coChi != null && !coChi.equals("")) {
                        companyWeightMoneyNumBO.setChiCo(coChi);
                    }
                    if (pdtClassId != null) {
                        QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                        cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                        cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                        cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                        List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                        List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                        QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                        cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                        List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                        companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                    }
                    if (product != null && product.size() > 0) {
                        companyWeightMoneyNumBO.setProduct(product);
                    }
                    if (startTime != null && !startTime.equals("")) {
                        companyWeightMoneyNumBO.setStartTime(startTime);
                    }
                    if (endTime != null && !endTime.equals("")) {
                        companyWeightMoneyNumBO.setEndTime(endTime);
                    }
                    if (truckNo != null && !truckNo.equals("")) {
                        companyWeightMoneyNumBO.setTruckNo(truckNo);
                    }
                    if (icCardNo != null && !icCardNo.equals("")) {
                        companyWeightMoneyNumBO.setIccardno(icCardNo);
                    }
                    if (plantId != null && !plantId.equals("")) {
                        companyWeightMoneyNumBO.setPlantId(plantId);
                    }
                    List<CompanyWeightMoneyNumVo> companyWeightMoneyNum = icWeightSheetsService.dailyStatistics(companyWeightMoneyNumBO);
                    for (CompanyWeightMoneyNumVo companyWeightMoneyNumVo : companyWeightMoneyNum) {
                        ProductWeightMoneyNumExportExcelEn companyWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        companyWeightMoneyNumExportExcelEn.setProduct(companyWeightMoneyNumVo.getProduct());
                        companyWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalMoney())));
                        companyWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalNum())));
                        companyWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalWeight())));
                        list.add(companyWeightMoneyNumExportExcelEn);
                    }
                    break;


                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        ProductWeightMoneyNumExportExcelEn productWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }


    public SzResult dailyStatisticsByCpy(String dateTime, String coSupply, String coSupplyChi, String co, String chiCo, Integer pdtClassId, List<String> product, String truckNo, String iccardno, Integer isEmpty, String cpyClass, String pdtClass, String token, String plantId) throws Exception {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
        if (coSupply != null && !coSupply.equals("")) {
            companyWeightMoneyNumBO.setCoSupply(coSupply);
        }
        if (coSupplyChi != null && !coSupplyChi.equals("")) {
            companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
        }
        if (co != null && !co.equals("")) {
            companyWeightMoneyNumBO.setCo(co);
        }
        if (chiCo != null && !chiCo.equals("")) {
            companyWeightMoneyNumBO.setChiCo(chiCo);
        }
        if (pdtClassId != null) {
            QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
            cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
            cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
            cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
            List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
            List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
            companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
        }
        if (product != null && product.size() > 0) {
            companyWeightMoneyNumBO.setProduct(product);
        }
        if (dateTime != null && !dateTime.equals("")) {
            String startTime = dateTime + " 00:00:00";
            String endTime = dateTime + " 23:59:59";
            companyWeightMoneyNumBO.setStartTime(startTime);
            companyWeightMoneyNumBO.setEndTime(endTime);
        }
        if (truckNo != null && !truckNo.equals("")) {
            companyWeightMoneyNumBO.setTruckNo(truckNo);
        }
        if (iccardno != null && !iccardno.equals("")) {
            companyWeightMoneyNumBO.setIccardno(iccardno);
        }

        if (isEmpty != null) {
            companyWeightMoneyNumBO.setIsEmpty(isEmpty);
        }
        if (plantId != null && !plantId.equals("")) {
            companyWeightMoneyNumBO.setPlantId(plantId);
        }
//        默认为不查询空车
        else {
            companyWeightMoneyNumBO.setIsEmpty(0);
        }
        List<CompanyWeightMoneyNumVo> companyWeightMoneyNum = icWeightSheetsService.dailyStatisticsByCpy(companyWeightMoneyNumBO);
        return SzResult.success(companyWeightMoneyNum);
    }


    @Async
    public Future<List<DailyStatisticsByCpyExportExcelEn>> dailyStatisticsByCpy(List<Map<String, Object>> fields, List<Map<String, Object>> ids, String filename, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, Integer pdtClassId, List<String> product, String startTime, String endTime, String truckNo, String icCardNo, String sheetName, HttpServletResponse response, String plantId, String coSupply, String coSupplyChi, String co, String coChi) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<DailyStatisticsByCpyExportExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        DailyStatisticsByCpyExportExcelEn productWeightMoneyNumExportExcelEn = DailyStatisticsByCpyExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("co")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("chiCo")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setChiCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtClass")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setPdtClass(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }
                    break;
                case "all":
                    CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
                    if (coSupply != null && !coSupply.equals("")) {
                        companyWeightMoneyNumBO.setCoSupply(coSupply);
                    }
                    if (coSupplyChi != null && !coSupplyChi.equals("")) {
                        companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
                    }
                    if (co != null && !co.equals("")) {
                        companyWeightMoneyNumBO.setCo(co);
                    }
                    if (coChi != null && !coChi.equals("")) {
                        companyWeightMoneyNumBO.setChiCo(coChi);
                    }
                    if (pdtClassId != null) {
                        QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                        cPdtClassQueryWrapper.eq("ID", pdtClassId);
//            PDTCLASS_DELETED=0
                        cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                        cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                        List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                        List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getId).collect(Collectors.toList());
                        QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                        cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                        List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                        companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                    }
                    if (product != null && product.size() > 0) {
                        companyWeightMoneyNumBO.setProduct(product);
                    }
                    if (startTime != null && !startTime.equals("")) {
                        companyWeightMoneyNumBO.setStartTime(startTime);
                    }
                    if (endTime != null && !endTime.equals("")) {
                        companyWeightMoneyNumBO.setEndTime(endTime);
                    }
                    if (truckNo != null && !truckNo.equals("")) {
                        companyWeightMoneyNumBO.setTruckNo(truckNo);
                    }
                    if (icCardNo != null && !icCardNo.equals("")) {
                        companyWeightMoneyNumBO.setIccardno(icCardNo);
                    }
                    if (plantId != null && !plantId.equals("")) {
                        companyWeightMoneyNumBO.setPlantId(plantId);
                    }
                    List<CompanyWeightMoneyNumVo> companyWeightMoneyNum = icWeightSheetsService.dailyStatisticsByCpy(companyWeightMoneyNumBO);
                    for (CompanyWeightMoneyNumVo companyWeightMoneyNumVo : companyWeightMoneyNum) {
                        DailyStatisticsByCpyExportExcelEn companyWeightMoneyNumExportExcelEn = DailyStatisticsByCpyExportExcelEn.builder().build();
                        companyWeightMoneyNumExportExcelEn.setProduct(companyWeightMoneyNumVo.getProduct());
                        companyWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalMoney())));
                        companyWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalNum())));
                        companyWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalWeight())));
                        companyWeightMoneyNumExportExcelEn.setCo(companyWeightMoneyNumVo.getCo());
                        companyWeightMoneyNumExportExcelEn.setChiCo(companyWeightMoneyNumVo.getChiCo());
                        companyWeightMoneyNumExportExcelEn.setPdtClass(companyWeightMoneyNumVo.getPdtClass());
                        list.add(companyWeightMoneyNumExportExcelEn);
                    }
                    break;


                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        DailyStatisticsByCpyExportExcelEn productWeightMoneyNumExportExcelEn = DailyStatisticsByCpyExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("co")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("chiCo")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setChiCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtClass")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setPdtClass(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }
}